<template>
  <el-dialog
      :width="600"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
export default {
  name: 'FormAdd',
  data() {
    return {
      apiKey: 'banner',
      dialogValue: false,
      ruleForm: {
      },
      defaultForm: {
        name: '',
        post_url: '',
        desc: '',
        status: 1,
        location: '',
        url: ''
      },
      rules: {
        location: [{ required: true, message: '不能为空' }],
      },
      formList: [
        {
          key: 'url',
          name: 'file',
          multiple: true,
          defaultValue: '',
          label: '文件（图片、视频）',
          colSpan: 12
        },
        {
          key: 'post_url',
          name: 'file',
          multiple: true,
          defaultValue: '',
          label: '封面（视频）',
          colSpan: 12
        },
        {
          key: 'location',
          name: 'select',
          label: '位置',
          colSpan: 12,
          options: [
            {
              name: '首页背景',
              value: '首页背景'
            },
            {
              name: '首页-3-Hour Sunset Fishing Cruise',
              value: '首页-3-Hour Sunset Fishing Cruise'
            },
            {
              name: '首页-Half-Day Hook, Dive, and Grill',
              value: '首页-Half-Day Hook, Dive, and Grill'
            },
            {
              name: '首页-Private Charters Booking',
              value: '首页-Private Charters Booking'
            },
            {
              name: '首页-视频',
              value: '首页-视频'
            },
            {
              name: '首页-轮播',
              value: '首页-轮播'
            },
            {
              name: '产品头部-3-Hour Sunset Fishing Cruise',
              value: '产品头部-3-Hour Sunset Fishing Cruise'
            },
            {
              name: '产品轮播-3-Hour Sunset Fishing Cruise',
              value: '产品轮播-3-Hour Sunset Fishing Cruise'
            },
            {
              name: '产品其他-3-Hour Sunset Fishing Cruise',
              value: '产品其他-3-Hour Sunset Fishing Cruise'
            },
            {
              name: '产品头部-Half-Day Hook, Dive, and Grill',
              value: '产品头部-Half-Day Hook, Dive, and Grill'
            },
            {
              name: '产品轮播-Half-Day Hook, Dive, and Grill',
              value: '产品轮播-Half-Day Hook, Dive, and Grill'
            },
            {
              name: '产品其他-Half-Day Hook, Dive, and Grill',
              value: '产品其他-Half-Day Hook, Dive, and Grill'
            },
            {
              name: '产品头部-Private Charters Booking',
              value: '产品头部-Private Charters Booking'
            },
            {
              name: '产品轮播-Private Charters Booking',
              value: '产品轮播-Private Charters Booking'
            },
            {
              name: '产品其他-Private Charters Booking',
              value: '产品其他-Private Charters Booking'
            },
            {
              name: 'about',
              value: 'about'
            },
            {
              name: 'team',
              value: 'team'
            },
            {
              name: 'boat-left',
              value: 'boat-left'
            },
            {
              name: 'boat-right',
              value: 'boat-right'
            },
            {
              name: 'contact',
              value: 'contact'
            },
            {
              name: 'apply',
              value: 'apply'
            },
            {
              name: 'faq',
              value: 'faq'
            },
            {
              name: 'gallery',
              value: 'gallery'
            },
          ]
        },
        {
          key: 'status',
          name: 'switch',
          label: '是否启用',
          activeValue: 1,
          inactiveValue: 0,
          colSpan: 12
        }
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http[this.apiKey].read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          name: '',
          src: '',
          link: '',
          status: 1,
          sort: 0
        }
      }
    },
    onSave() {
      http[this.apiKey][this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
